package it.unisa.javaxtool.client;


import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.scene.Parent;
import javafx.scene.input.MouseEvent;


public class DraggableNode extends Parent{
	//ATTRIBUTES
	//X AND Y position of Node
	double x = 0;
	double y = 0;

	//X AND Y position of mouse
	double mousex=0;
	double mousey=0;
	/**
	 * the communication object
	 */
	private DraggableNodeService service;
	
	
	//To make this function accessible for other Class
	public ObservableList getChildren(){
		return super.getChildren();
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public DraggableNode(){
		super();
		//EventListener for MousePressed
		onMousePressedProperty().set(new EventHandler<MouseEvent>(){

			public void handle(final MouseEvent event) {
				System.out.println("handle Mouse Event");
				//record the current mouse X and Y position on Node
				mousex = event.getSceneX();
				mousey= event.getSceneY();

				//get the x and y position measure from Left-Top
				x = getLayoutX();
				y = getLayoutY();
				System.out.println(" mouse event mousex=" + mousex + " mousey="+ mousey );
			}
		});
		//Event Listener for MouseDragged
		onMouseDraggedProperty().set(new EventHandler<MouseEvent>() {

			public void handle(final MouseEvent event) {
				System.out.println("handle Mouse Event Dragged");
				//Get the exact moved X and Y
				x += event.getSceneX()-mousex ;
				y += event.getSceneY()-mousey ;

				//set the position of Node after calculation
				setLayoutX(x);
				setLayoutY(y);

				//again set current Mouse x AND y position
				mousex = event.getSceneX();
				mousey= event.getSceneY();
				
				//service.sendNodeDraggable(mousex, mousey);
			}
		});
		
		onMouseReleasedProperty().set(new EventHandler<MouseEvent>() {

			@Override
			public void handle(final MouseEvent event) {
				// TODO Auto-generated method stub
				System.out.println("handle Mouse Event Released!!" );
				//Get the exact moved X and Y
				x += event.getSceneX()-mousex ;
				y += event.getSceneY()-mousey ;
				
				//set the position of Node after calculation
				setLayoutX(x);
				setLayoutY(y);
				
				mousex = event.getSceneX();
				mousey= event.getSceneY();
				System.out.println("mousex=" + mousex + " mousey="+ mousey );
				//service.sendNodeDraggable(mousex, mousey);
			}
			
		});
	}
	

	public DraggableNodeService getService() {
		return service;
	}

	public void setService(DraggableNodeService service) {
		this.service = service;
	}
}
